﻿using AppFlat.Application.IService;
using AppFlat.Domain.IRespostroy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace AppFlat.Application.Service
{
    public class BaseService<T> 
    {
        public IBaseRespostory<T> Repostory { get; set; }
        public IUnitOfWork UnitOfWork { get; set; }


        public async Task<bool> AddAsync(T model)
        {
            Repostory.Add(model);
            return await UnitOfWork.SaveChangesAsync();
        }

        public async Task<bool> DeleteAsync(T model)
        {
            Repostory.Delete(model);
            return await UnitOfWork.SaveChangesAsync();
        }

        public IQueryable<T> GetEntities<OrderField>(int pageIndex, int pageSize, out int total, Expression<Func<T, bool>> whereLambda, Expression<Func<T, OrderField>> orderBy, bool isAsc)
        {
            return Repostory.GetEntities(pageIndex, pageSize, out total, whereLambda, orderBy, isAsc);
        }

        public IQueryable<T> GetEntities(Expression<Func<T, bool>> lambdaWhere = null)
        {
            return Repostory.GetEntities(lambdaWhere);
        }

        public async Task<T> GetEntityAsync(Expression<Func<T, bool>> whereLambda)
        {
            return await Repostory.GetEntityAsync(whereLambda);
        }

        public async Task<bool> UpdateAsync(T model)
        {
            Repostory.Update(model);
            return await UnitOfWork.SaveChangesAsync();
        }
    }
}
